package com.elcl.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.elcl.storage.ApplicationCache;
import com.elcl.util.ListUtils;

import java.util.List;

/**
 * Created by lichunlin on 16/7/5.
 * expandablelistview的baseadapter
 */
public abstract class BaseExpandableLvAdaper<T> extends BaseExpandableListAdapter {

    protected List<ExpandableAdapterTitleEntity> list_title;//顶部的头
    protected List<List<T>> list_detail;

    protected int idGroupLayout;
    protected int idChildLayout;

    public BaseExpandableLvAdaper(List<ExpandableAdapterTitleEntity> list_title, List<List<T>> list_detail, int idGroupLayout, int idChildLayout) {
        this.list_title = list_title;
        this.list_detail = list_detail;
        this.idGroupLayout = idGroupLayout;
        this.idChildLayout = idChildLayout;
    }

    @Override
    public int getGroupCount() {
        if (ListUtils.isEmpty(list_title)) {
            return 0;
        } else {
            return list_title.size();
        }
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (ListUtils.isEmpty(list_title)) {
            return 0;
        } else {
            return list_detail.get(groupPosition).size();
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list_title.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list_detail.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = LayoutInflater.from(ApplicationCache.context).inflate(idGroupLayout, null);
        }
        return getGroupItemView(groupPosition, isExpanded, convertView, parent);
    }

    public abstract View getGroupItemView(int position, boolean isExpanded, View convertView, ViewGroup parent);

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = LayoutInflater.from(ApplicationCache.context).inflate(idChildLayout, null);
        }
        return getChildItemView(groupPosition,childPosition,isLastChild,convertView,parent);
    }

    public abstract View getChildItemView(int groupPosition,int childPosition, boolean isLastChild, View convertView, ViewGroup parent);

}
